﻿using DX_Recognize.Common;
using DX_Recognize.Entitys;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static DX_Recognize.Utils.ConvertUtils;

namespace DX_Recognize.View
{
    public partial class SpringHoleWindow : Form
    {
        public SHoleProcessInfo infos = null;
        public event Func<SHoleProcessInfo> SAssemFunc;
        public event Func<SHoleProcessInfo> SProjectionFunc;
        public SpringHoleWindow()
        {
            InitializeComponent();
            this.Hide();
        }

        public void ResetUI()
        {
            infos = AssemblyShareData.sHoleProcessInfo;
            this.DepthBox.Text = infos.depth.ToString();
            this.RPPosCheckBox.Checked = infos.hasRPPos;
            this.NumBox.Text = infos.num.ToString();
            this.DimBox.Text = infos.dim.ToString();
            this.PosLBox.Text = infos.posL.ToString();
            this.PosWBox.Text = infos.posW.ToString();
        }
        public void SetUI()
        {
            ResetUI();
            ReShow();
        }

        private void ReShow()
        {
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;
            this.Show();
        }

        private void CancelBtn_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void ComfirmBtn_Click(object sender, EventArgs e)
        {
            infos.hasRPPos = this.RPPosCheckBox.Checked;
            Boolean F1 = Double.TryParse(this.DimBox.Text, out double Dim);
            if (F1)
                infos.dim = Dim;
            Boolean F2 = Int32.TryParse(this.NumBox.Text, out int Num);
            if (F2)
                infos.num = Num;
            Boolean F3 = Double.TryParse(this.PosLBox.Text, out double PosL);
            if (F3)
                infos.posL = PosL;
            Boolean F4 = Double.TryParse(this.PosWBox.Text, out double PosW);
            if (F4)
                infos.posW = PosW;
            Boolean F5 = Double.TryParse(this.DepthBox.Text, out double Depth);
            if (F5)
                infos.depth = Depth;
            this.Hide();
        }

        private void SpringHoleWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        private void SAssemBtn_Click(object sender, EventArgs e)
        {
            this.Hide();
            SHoleProcessInfo Res = SAssemFunc?.Invoke();
            AssemblyShareData.sHoleProcessInfo.depth =  Res.depth;
            AssemblyShareData.sHoleProcessInfo.dim =  Res.dim;
            SetUI();
        }

        private void SProjectionBtn_Click(object sender, EventArgs e)
        {
            this.Hide();
            SHoleProcessInfo Res = SProjectionFunc?.Invoke();
            AssemblyShareData.sHoleProcessInfo.num = Res.num;
            AssemblyShareData.sHoleProcessInfo.posL = Res.posL;
            AssemblyShareData.sHoleProcessInfo.posW = Res.posW;
            AssemblyShareData.sHoleProcessInfo.hasRPPos = Res.hasRPPos;
            SetUI();
        }
    }
}
